﻿using BrnMall.Manager.Domain.Product;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.Product
{
    /// <summary>
    /// 产品的日期价管理
    /// </summary>
    public static class ProductPriceManager
    {

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Add(ProductPrice model)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(model);
            }
            if (obj != null)
                return true;
            return false;
        }

        public static bool Update(ProductPrice model)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Update(model);
            }
            return count > 0;
        }

        public static bool Delete(int priceId)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute(string.Format("delete bma_ProductPrice where priceid={0}", priceId));
            }
            return count > 0;
        }

        /// <summary>
        /// 获取指定的日期价格
        /// </summary>
        /// <param name="pids">产品ID</param>
        /// <param name="startTime">开始日期 ,格式：20180101</param>
        /// /// <param name="endTime">开始日期 ,格式：20180101</param>
        /// <param name="pageSize">最大返回行数</param>
        /// <returns></returns>
        public static List<ProductPrice> GetListByDate(int pid, string startTime, string endTime)
        {
            List<ProductPrice> list = null;
            using (var db = new QDMallDatabase())
            {
                list = db.Fetch<ProductPrice>(string.Format("select * from bma_ProductPrice where pid={0} and appointDay>={1} and appointDay<{2}", pid, startTime, endTime));
            }
            return list;
        }

        /// <summary>
        /// 产品价格列表
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static IList<ProductPrice> GetList(int pid, int pageSize = 100)
        {
            IList<ProductPrice> list = null;
            using (var db = new QDMallDatabase())
            {
                list = db.Fetch<ProductPrice>(string.Format("select top {0} * from bma_ProductPrice where pid={1}", pageSize, pid));
            }
            return list;
        }

        /// <summary>
        /// 按日期获取价格
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public static List<ProductPrice> GetDayList(string ids, string day)
        {
            List<ProductPrice> list = null;
            using (var db = new QDMallDatabase())
            {
                if (ids.Contains(","))
                    list = db.Fetch<ProductPrice>(string.Format("select * from bma_ProductPrice where appointDay={0} and pid in ({1})", day, ids));
                else
                    list = db.Fetch<ProductPrice>(string.Format("select * from bma_ProductPrice where appointDay={0} and  pid={1}", day, ids));
            }
            return list;
        }
    }
}